<?php
//
//	file: includes/aucp/aucp_functions.php
//	begin: 19/01/2008
//	$Author: christopher $
//	$Revision: 29 $
//	$Date: 2008-07-19 15:59:35 +0000 (Sat, 19 Jul 2008) $
//	license: http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
//
//	description: aucp global functions

if ( !defined('IN_PHPBB') )
{
	die('Hacking attempt');
}

function _check_install($field, $value)
{
	global $config;

	if (file_exists(phpbb_realpath($config->root . 'installer')) && ($config->data[$field] != $value))
	{
		header('Location: ' . $config->url('installer/install'));
		exit;
	}
	if ( !$config->data[$field] )
	{
		return false;
	}
	else
	{
		return true;
	}
}

function _add_lang($files, $cpname='')
{
	global $user;

	$cpname = (empty($cpname)) ? $file : $cpname;
	$file = (is_array($files)) ? $files : explode(',', $files);
	$user->use_lang_file($file);
	$user->use_lang($cpname);

	return;
}

// get_usernames : go through a list of usernames and return an array of successfully matched user_ids
function get_usernames($users)
{
	global $db;

	if(is_array($users))
	{
		implode(', ', $users);
	}

	$ret = array();
	$sql = 'SELECT user_id
			FROM ' . USERS_TABLE . '
			WHERE username IN (' . $users . ')';
	$result = $db->sql_query($sql, false, __LINE__, __FILE__);
	while ( $row = $db->sql_fetchrow($result) )
	{
		$ret[] = $row['user_id'];
	}
	$db->sql_freeresult($result);

	return $ret;
}

if ( !defined('get_var') )
{
	// get_var : read $_POST/GET vars
	function get_var($var_name, $default, $multibyte = false)
	{
		global $HTTP_POST_VARS, $HTTP_GET_VARS;

		$request_var = (isset($HTTP_POST_VARS[$var_name])) ? $HTTP_POST_VARS : $HTTP_GET_VARS;
		if ( !isset($request_var[$var_name]) || (is_array($request_var[$var_name]) && !is_array($default)) || (is_array($default) && !is_array($request_var[$var_name])) )
		{
			return is_array($default) ? array() : $default;
		}

		// get type from default
		if ( !is_array($default) )
		{
			$type = gettype($default);
		}
		else
		{
			list($key_type, $type) = each($default);
			$type = gettype($type);
			$key_type = gettype($key_type);
		}

		// get value
		$var = $request_var[$var_name];
		if ( !is_array($var) )
		{
			_set_var($var, $var, $type, $multibyte);
		}
		else
		{
			$_var = $var;
			$var = array();
			foreach ($_var as $k => $v)
			{
				_set_var($k, $k, $key_type);
				if ( !is_array($v) )
				{
					_set_var($var[$k], $v, $type, $multibyte);
				}
				else
				{
					foreach ($v as $_k => $_v)
					{
						_set_var($_k, $_k, $key_type);
						_set_var($var[$k][$_k], $_v, $type, $multibyte);
					}
				}
			}
		}
		return $var;
	}

	// _set_var : type cast for get_var()
	function _set_var(&$result, $var, $type, $multibyte = false)
	{
		settype($var, $type);
		$result = $var;

		if ($type == 'string')
		{
			$result = trim(htmlspecialchars(str_replace(array("\r\n", "\r", '\xFF'), array("\n", "\n", ' '), $result)));
			// 2.0.x is doing addslashes on all variables
			$result = stripslashes($result);
			if ($multibyte)
			{
				$result = preg_replace('#&amp;(\#[0-9]+;)#', '&\1', $result);
			}
		}
	}
}

?>